{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c163dbf4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f7bcae93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "a1 = np.array([1,2,3])\n",
    "print(a1.ndim)\n",
    "a2 = np.array([[1,2,3],[4,5,6]])\n",
    "print(a2.ndim)\n",
    "a3 = np.array([\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6]\n",
    "    ],\n",
    "    [\n",
    "        [7,8,9],\n",
    "        [10,11,12]\n",
    "    ]\n",
    "])\n",
    "print(a3.ndim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "98a8a670",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3,)\n",
      "(2, 3)\n",
      "(2, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "print(a1.shape)\n",
    "print(a2.shape)\n",
    "print(a3.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b11a8b4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5  6]\n",
      " [ 7  8  9 10 11 12]]\n",
      "(2, 6)\n"
     ]
    }
   ],
   "source": [
    "a4 = a3.reshape((2,6))\n",
    "print(a4)\n",
    "print(a4.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b65b07d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2  3  4  5  6  7  8  9 10 11 12]\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "#a5 = a3.reshape((1,12))\n",
    "# a5 = a3.reshape((12,))\n",
    "a5 = a3.flatten()\n",
    "print(a5)\n",
    "print(a5.ndim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1e40c8b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n"
     ]
    }
   ],
   "source": [
    "count = a3.size\n",
    "print(count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6a8b1089",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "int32\n",
      "48\n"
     ]
    }
   ],
   "source": [
    "itemsize = a3.itemsize\n",
    "print(itemsize)\n",
    "print(a3.dtype)\n",
    "print(itemsize*a3.size)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1a53857",
   "metadata": {},
   "source": [
    "# 总结\n",
    "1.数组一般达到三维就已经很复杂了，不太方便计算了，所以我们一般都会把三维以上的数组转换为2维数组来计算。  \n",
    "2.通过ndarray.ndim可以看到数组的维度   \n",
    "3.通过ndarray.shape可以看到数组的形状（几行几列），shape时一个元组，里面有几个元素代表是几维数组。   \n",
    "4.通过ndarray.reshape可以修改数组的形状。条件只有一个，就是修改后的形状的元素个数必须和原来的个数一致。比如原来是(2,6),那么修改完成后可以变成(3,4),但是不能变为(1,4)。reshape不会修改原来数组的形状，它只会将修改后的结果返回。   \n",
    "5.通过ndarray.size可以看到数组总共有多少个元素   \n",
    "6.通过ndarray.itemsize可以看到数组中每个元素所占内存的大小，单位是字节  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
